package com.blrs.web.oss;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
@Data
public class ZosClient {

    @Value("${zos.oss.endpoint}")
    private String endPoint;
    @Value("${zos.oss.accessKeyId}")
    private String accessKey;
    @Value("${zos.oss.accessKeySecret}")
    private String secretKey;
    @Value("${zos.oss.bucketName}")
    private String bucketName;

    @Bean
    public AmazonS3 getS3Client() {
        AmazonS3 s3Client;
        // 使用凭据和配置建立连接
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        ClientConfiguration awsClientConfig = new ClientConfiguration();
        // 使用V2签名时，采用"S3SignerType"
        awsClientConfig.setSignerOverride("S3SignerType");
        // 使用V4签名时，采用"AWSS3V4SignerType"
        //awsClientConfig.setSignerOverride("AWSS3V4SignerType");
        // 连接默认使用HTTPS协议，使用HTTP协议连接时需要显式指定
        awsClientConfig.setProtocol(Protocol.HTTP);

        s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withClientConfiguration(awsClientConfig)
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, ""))
                .disableChunkedEncoding()
                .build();

        return s3Client;
    }




}